import Vue from 'vue'
import Vuex from 'vuex'
import { Toast } from 'vant';
// 导入日志插件
import createLogger from 'vuex/dist/logger'
//导入持久化插件
import createPersistedState from "vuex-persistedstate";

// 导入接口
import { shop_favlist, shop_fav, shop_unfav, shopcart_list, shopcart_add, shopcart_clear, shopcart_sub, address_add, address_list } from '../utils/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //收藏列表
    collectlist: [],
    // 搜索历史
    historyArr: [],
    //购物车列表
    shopcartlist: [],
    // 地址列表 
    addlist: [],
  },
  getters: {

  },
  mutations: {
    //收藏一个店铺
    collect(state, payload) {
      state.collectlist.push(payload);
    },
    //取消收藏一个店铺
    cancel_collect(state, payload) {
      var index = state.collectlist.findIndex((item) => {
        return item.mtWmPoiId == payload.mtWmPoiId;
      })
      if (index != -1) {
        state.collectlist.splice(index, 1);
      }
    },
    // 保存收藏列表
    save_favlist(state, payload) {
      state.collectlist = payload
    },
    // 搜索历史
    historylist(state, payload) {
      state.historyArr.unshift(payload);
      if (state.historyArr.length > 9) {
        state.historyArr.pop()
      }
    },
    // 保存购物车列表
    request_shopcartlist(state, payload) {
      state.shopcartlist = payload
    },
    // 清空
    shopcart_clear(state) {
      state.shopcartlist = [];
    },
    shopcart_add(state, payload) {//数量+1
      let index = state.shopcartlist.findIndex((item) => {
        return item.id == payload.id;
      })
      if (index != -1) {
        state.shopcartlist[index].count++;
        console.log(state.shopcartlist[index].count);
      } else {
        // 拷贝一份
        let good = JSON.parse(JSON.stringify(payload));
        good.count = 1;
        state.shopcartlist.push(good)
      }
    },
    shopcart_sub(state, payload) {//数量-1
      let index = state.shopcartlist.findIndex((item) => {
        return item.id == payload.id;
      })
      if (index != -1) {
        state.shopcartlist[index].count--;
        if (state.shopcartlist[index].count < 1) {
          state.shopcartlist.splice(index)
        }
      }
    },
    // 保存到本地 地址列表
    address_list(state, payload) {
      state.addlist = payload
    },
    // 新建地址
    address_add(state, payload) {
      let info = JSON.parse(JSON.stringify(payload))
      state.addlist.push(info);
    }
  },
  actions: {
    // 获取收藏列表
    request_favlist(store) {
      // 获取本地存储中用户的id
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      // // 发请求，获取当前用户的收藏列表
      console.log(userid);
      shop_favlist({ userid }).then((res) => {
        console.log(res.data.list);
        store.commit('save_favlist', res.data.list)
      })
    },
    // 发请求收藏
    request_fav(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      shop_fav({ userid, shopid: payload.mtWmPoiId }).then((res) => {
        if (res.data.code == 200) {
          // 收藏到vuex
          Toast('收藏成功');
          store.commit('collect', payload);
        }
      })
    },
    // 发送请求取消收藏
    request_unfav(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      shop_unfav({ userid, shopid: payload.mtWmPoiId }).then((res) => {
        if (res.data.code == 200) {
          Toast('取消收藏');
          store.commit('cancel_collect', payload);
        }
      })
    },
    // 请求购物车列表
    request_shopcartlist(store) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      // 发请求
      shopcart_list({ userid }).then((res) => {
        if (res.data.code == 200) {
          console.log(res.data.list);
          store.commit('request_shopcartlist', res.data.list);
        }
      })
    },
    // 清空购物车
    request_shopcart_clear(store) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      shopcart_clear({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('shopcart_clear');
        }
      })
    },
    // 请求数量加1
    request_shopcart_add(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      // 商品+1
      shopcart_add({ userid, goodid: payload.id }).then((res) => {
        if (res.data.code == 200) {
          store.commit('shopcart_add', payload);
        }
      })
    },
    // 请求数量减1
    request_shopcart_sub(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      // 商品+1
      shopcart_sub({ userid, goodid: payload.id }).then((res) => {
        if (res.data.code == 200) {
          store.commit('shopcart_sub', payload);
        }
      })
    },
    // 发情请求 获取列表 
    request_address_list(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      address_list({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('address_list', payload);
        } else {
          Toast('获取失败');
        }
      })

    },
    // 发情请求 新建地址 
    request_address_add(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      address_add({ userid, address: payload.user_address, phone: payload.user_phone, name: payload.user_name }).then((res) => {
        if (res.data.code == 200) {
          Toast.success('添加成功');
          store.commit('address_add', payload);
        } else {
          Toast.fail('添加失败');
        }
      })
    },


  },
  modules: {
  },
  plugins: [createLogger(), createPersistedState()]
})



